<?php
/**
 * 广告模型管理
 * @author cjli
 *
 */
class AdvModel extends Model
{
	public static function showType()
	{
		return array(
			'1' => '图片',
			'2' => 'flash',
			'3' => '文字',
			'4' => '代码',
		);
	}
	/**
	 * 获取广告轮播列表
	 * @return array
	 */
	public static function getSlideList()
	{
		if(S('slide_list')){
			$slideList = S('slide_list');
		}else{
			$slide_db = D('adv_slide');
			$slideList = $slide_db->order('listorder DESC')->where('visible=1')->select();
			S('slide_list', $slideList);
		}
		return $slideList;
	}
	
	/**
	 * 获取广告位列表
	 * @return array
	 */
	public static function getPositionList()
	{
		if(S('position_list')){
			$positionList = S('position_list');
		}else{
			$position_db = D('adv_position');
			$result = $position_db->where('status = 1')->select();
			foreach ($result as $position){
				$positionList[$position['id']] = $position;
			}
			S('position_list', $positionList);
		}
		return $positionList;
	}
	
	/**
	 * 根据广告位ID获取广告位信息
	 * @param int $id 广告位ID
	 * @return array
	 */
	public static function getPositionInfo($id)
	{
		$positionList = self::getPositionList();
		return $positionList[$id] ? $positionList[$id] : false;
	}
	
	/**
	 * 根据广告位ID获取广告列表
	 * @param int $id 广告位ID
	 * @return array
	 */
	public static function getAdvListByPositionId($id)
	{
		$now = date("Y-m-d", time());
		static $list;
		$listid = $now.'-'.$id;
		if(!$list[$listid]){
			$adv_db = D('adv_manage');
			$where['start_time'] = array('elt', $now);
			$where['end_time'] = array('gt', $now);
			$where['position_id'] = $id;
			$list[$listid] = $adv_db->where($where)->order('listorder DESC')->select();
		}
		return $list[$listid];
	}
	
	/**
	 * 获取广告列表，设置时间无效
	 * @return array
	 */
	public static function getAdvList()
	{
		if(S('adv_list')){
			$advList = S('adv_list');
		}else{
			$adv_db = D('adv_manage');
			$result = $adv_db->order('listorder DESC')->select();
			foreach ($result as $adv){
				$advList[$adv['id']] = $adv;
				if($adv['script_id']){
					$advList[$adv['script_id']] = $adv;
				}
			}
			S('adv_list', $advList);
		}
		return $advList;
	}
	
	/**
	 * 获取广告信息
	 * @param string $id 广告标识或者广告ID号
	 * @return array
	 */
	public static function getAdvInfo($id)
	{
		$advList = self::getAdvList();
		return $advList[$id] ? $advList[$id] : false;
	}
}